<?php

namespace plugin\admin\app\controller;

use app\traits\DataProcessor;
use plugin\admin\app\common\Auth;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\Store;
use support\Request;
use support\Response;
use plugin\admin\app\model\StoreApply;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 开行申请 
 */
class StoreApplyController extends Crud
{
    
    /**
     * @var StoreApply
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new StoreApply;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('store-apply/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('store-apply/insert');
    }

    protected function updateInput(Request $request): array
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);
        $data = $this->inputFilter($request->post());
        if (!Auth::isSupperAdmin() && $this->dataLimit && !empty($data[$this->dataLimitField])) {
            $admin_id = $data[$this->dataLimitField];
            if (!in_array($admin_id, Auth::getScopeAdminIds(true))) {
                throw new BusinessException('无数据权限');
            }
        }
        $password_filed = 'password';
        if (isset($data[$password_filed])) {
            // 密码为空，则不更新密码
            if ($data[$password_filed] === '') {
                unset($data[$password_filed]);
            } else {
                $data[$password_filed] = Util::passwordHash($data[$password_filed]);
            }
        }

        DataProcessor::processSwitch($data, 'status');

        $storeApplyInfo = StoreApply::find($id);

        if (!$data['status']) {
            $data['status'] = 2;
        }

        if ($data['status'] == 1) {
            Store::insert([
               'user_id' =>  $storeApplyInfo['user_id'],
                'category_id' => $storeApplyInfo['category_id'],
                'apply_id' => $storeApplyInfo['id'],
                'name' => $storeApplyInfo['name'],
                'company_name' => $storeApplyInfo['name'],
                'business_start_time' => $storeApplyInfo['business_start_time'],
                'business_end_time' => $storeApplyInfo['business_end_time'],
                'contact_name' => $storeApplyInfo['legal_name'],
                'contact_mobile' => $storeApplyInfo['mobile'],
                'longitude' => $storeApplyInfo['longitude'],
                'latitude' => $storeApplyInfo['latitude'],
                'province' => $storeApplyInfo['province'],
                'city' => $storeApplyInfo['city'],
                'district' => $storeApplyInfo['district'],
                'address' => $storeApplyInfo['address'],
                'thumb' => $storeApplyInfo['thumb'],
                'desc' => $storeApplyInfo['content'],
                'created_time' => date('Y-m-d H:i:s')
            ]);
        }

        unset($data[$primary_key]);
        return [$id, $data];
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('store-apply/update');
    }

}
